﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Servers
{
    public class StepServers
    {
        private readonly IRepository<wf_flowstepinfo> wf_flowstepinfoRepository;
        private readonly IRepository<wf_flowstepnext> wf_flowstepnextRepository;
        public StepServers()
        {
            wf_flowstepinfoRepository = new DapperRepository<wf_flowstepinfo>();
            wf_flowstepnextRepository = new DapperRepository<wf_flowstepnext>();
        }

        public List<wf_flowstepinfo> GetData(string flowid)
        {
            var sql = "select * from wf_flowstepinfo where FLOWID=@FLOWID";
            return wf_flowstepinfoRepository.SqlWithParams(sql, new { FLOWID = flowid }).ToList();
        }

        public wf_flowstepinfo GetModelById(string id)
        {
            return wf_flowstepinfoRepository.GetById(id);
        }


        internal void Add(wf_flowstepinfo model)
        {
            wf_flowstepinfoRepository.Insert(model);
        }

        internal void Update(wf_flowstepinfo model)
        {
            wf_flowstepinfoRepository.Update(model);
        }

        internal void Del(string id)
        {
            wf_flowstepinfoRepository.DeleteByIds(id);
        }

        internal List<SelectListItem> GetNames(string id)
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = wf_flowstepinfoRepository.SqlWithParamsDynamic("select a.id Value,a.Name Text from wf_flowstepinfo a  where flowid=(select flowid from wf_flowstepinfo where ID=@ID )", new { ID = id });
            tables.Clear();
            //tables.Add(new SelectListItem { Value = "0", Text = "表单主分类" });
            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal List<wf_flowstepnext> GetNextData(string stepid)
        {
            var sql = "select * from wf_flowstepnext where STEPID=@STEPID";
            var relist = wf_flowstepnextRepository.SqlWithParams(sql, new { STEPID = stepid });
            return relist?.ToList();
        }

        internal void AddNextStep(wf_flowstepnext model)
        {
            //添加下一步骤
            wf_flowstepnextRepository.Insert(model);
            //更新next里的下一步骤
            var sql = "select '→'+ CAST(NEXTSTEPNO AS VARCHAR) from [dbo].[wf_flowstepnext]  n where n.stepid= @ID for xml path('')";
            var  stepid = wf_flowstepnextRepository.SqlWithParamsObject(sql, new { ID = model.STEPID });
            sql = "select CAST(NEXTTEXT AS VARCHAR)   + '§' from [dbo].[wf_flowstepnext]  n where n.stepid= @ID for xml path('')";
            var steptext = wf_flowstepnextRepository.SqlWithParamsObject(sql, new { ID = model.STEPID });
            var m=wf_flowstepinfoRepository.GetById(model.STEPID);            
            m.NEXTSTEP = stepid?.ToString();
            m.NEXTTEXT = steptext?.ToString().TrimEnd('§');
            wf_flowstepinfoRepository.Update(m);
        }

        internal List<SelectListItem> GetStepNames(string id)
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = wf_flowstepinfoRepository.SqlWithParamsDynamic("select a.id Value,a.Name Text from wf_flowstepinfo a  where flowid=@ID ", new { ID = id });
            tables.Clear();
            //tables.Add(new SelectListItem { Value = "0", Text = "表单主分类" });
            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal void NextDel(string id)
        {
            var sql = "select stepid from [dbo].[wf_flowstepnext] where ID=@id";
            var stepid = wf_flowstepnextRepository.SqlWithParamsObject(sql, new { ID = id });//取
            //删除下一步骤
            wf_flowstepnextRepository.DeleteByIds(id);
            //更新next里的下一步骤
            sql = "select  '→'+ CAST(NEXTSTEPNO AS VARCHAR)    from [dbo].[wf_flowstepnext]  n where n.stepid=@ID for xml path('')";
            var stepids = wf_flowstepnextRepository.SqlWithParamsObject(sql, new { ID = stepid.ToString() });
            sql = "select CAST(NEXTTEXT AS VARCHAR)   + '§' from [dbo].[wf_flowstepnext]  n where n.stepid=@ID  for xml path('')";
            var steptexts = wf_flowstepnextRepository.SqlWithParamsObject(sql, new { ID = stepid.ToString() });
            var m = wf_flowstepinfoRepository.GetById(stepid);
            m.NEXTSTEP = stepids.ToString();
            m.NEXTTEXT = steptexts.ToString().TrimEnd('§');
            wf_flowstepinfoRepository.Update(m);
        }
    }
}